package com.zhenwei.zscm.repo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhenwei.zscm.common.utils.Linq;
import com.zhenwei.zscm.entity.ApiInfoEntity;
import com.zhenwei.zscm.mapper.IApiInfoMapper;
import com.zhenwei.zscm.mappings.ApiInfoMapping;
import com.zhenwei.zscm.model.dto.apiInfo.ApiInfoDTO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ApiInfoRepo extends ServiceImpl<IApiInfoMapper, ApiInfoEntity> {

    @Transactional(rollbackFor = Exception.class)
    public boolean insert(List<ApiInfoDTO>list){
        try {
            baseMapper.truncate();
        } catch (Exception e) {
            var u = new LambdaUpdateWrapper<ApiInfoEntity>();
            u.ge(ApiInfoEntity::getId, 0);
            this.remove(u);
        }
        List<ApiInfoEntity> entityList = Linq.mapList(list, ApiInfoMapping.INSTANCE::dto2Entity);
        return this.saveBatch(entityList);
    }

    public List<ApiInfoDTO> all(){
        var q = new LambdaQueryWrapper<ApiInfoEntity>()
                .orderByAsc(ApiInfoEntity::getApiGroup)
                .orderByAsc(ApiInfoEntity::getApiDesc)
                .orderByAsc(ApiInfoEntity::getId);
        List<ApiInfoEntity> list = this.list(q);
        return Linq.mapList(list, ApiInfoMapping.INSTANCE::entity2Dto);
    }
}
